home *** CD-ROM | disk | FTP | other *** search
- /*
- File: Clipboard.c
-
- Contains: Clipboard support for simple text application.
-
- Version: SimpleText 1.4 or later
-
- Written by: Tom Dowdy
- DAL = Dave Lyons
-
- Copyright: © 1993-1997 by Apple Computer, Inc., all rights reserved.
-
- File Ownership:
-
- DRI: Tom Dowdy
-
- Other Contact: Jim Negrette
-
- Technology: Macinotosh Graphics Group
-
- Writers:
-
- (dmp) Dave Polaschek
- (ted) Tom Dowdy
- (TD) Tom Dowdy
-
- Change History (most recent first):
-
- $Log: Clipboard.c,v $
- Revision 1.10 1999/05/01 01:52:24 jiarocci
- Comment out grafprocs so we build against latest CarbonLib interfaces on 8.x.
-
- Revision 1.9 1999/02/16 00:41:37 christ
- Integrate document proxy icon support
-
- Revision 1.8 1998/11/11 22:28:52 wilkes
- Fixed various problems caused by the interface changes made by Nitin earlier,
- mostly involving static RoutineDescriptors...
-
- Revision 1.7 1998/10/14 18:52:45 voas
- Eliminate all warnings. Get working with top of tree.
-
- Revision 1.6 1998/09/15 18:59:42 jiarocci
- SimpleText now builds with -DTARGET_CARBON=1. Still needs further cleanup.
-
- Revision 1.5 1998/09/12 23:07:57 voas
- Use InvalWindowRect instead of InvalRect.
-
- Revision 1.4 1998/03/30 22:12:24 mkellner
- Update to use new GetQDxxxx macros for qd.globals
-
- Revision 1.3 1998/03/21 04:13:11 mkellner
- Add a define for qd_screenBits for qd.screenBits.
- Replaced qd.screenBits with qd_screenBits
-
- Revision 1.2 1998/03/20 03:19:52 mkellner
- change qd.thePort to FrontWindow()
- add SysEnvirons
-
- Revision 1.1.1.1 1998/03/18 22:56:06 ivory
- Initial checkin of SimpleText.
-
-
- 2 7/29/97 2:07 PM Tom Dowdy
- Removed all of the old and boring refs
-
- 1 7/28/97 11:11 AM Duane Byram
- first added to Source Safe project
-
- <5> 9/9/96 dmp staticfy local functions to eliminate warnings in MWC.
- <4> 5/31/96 ted spelling FAT correctly
- <4> 5/31/96 ted adding PPC, FAT, and NuKernel builds
- <3> 11/2/95 ted BlockMoveData
- <2> 10/2/95 TD adding support for SC compiler
- <1> 8/21/95 TD First checked in.
- <8> 9/8/94 DAL Added do-nothing routines ClipboardKeyEvent and
- ClipboardGetBalloon, to avoid strange balloon & keypress message
- for the Clipboard window.
-
- */
-
- #include "MacIncludes.h"
-
- #include "Clipboard.h"
-
-
- // --------------------------------------------------------------------------------------------------------------
- // GLOBALS FOR THIS FILE ONLY
- // --------------------------------------------------------------------------------------------------------------
- static Handle gScrapHandle;
- static long gCurrentOffset;
- #if USE_QD_GRAFPROCS
- static CQDProcs gSavedProcs;
- static CQDProcs gMyProcs;
- static CQDProcs gMyColorProcs;
- #endif
-
- // --------------------------------------------------------------------------------------------------------------
- // INTERNAL ROUTINES
- // --------------------------------------------------------------------------------------------------------------
- static pascal void GetPICTData(Ptr dataPtr, short byteCount)
- /*
- replacement for the QuickDraw bottleneck routine
- */
- {
- long longCount = byteCount;
-
- BlockMoveData((*gScrapHandle)+gCurrentOffset, dataPtr, longCount);
- gCurrentOffset += longCount;
-
- } // GetPICTData
-
-
- // --------------------------------------------------------------------------------------------------------------
-
- static OSErr DrawPictureFromHandleAndOffset(
- Rect * pWhereToDraw, // draw picture at this location
- Handle sourceHandle, // handle containing data
- long sourceOffset) // offset within handle to start at
- {
- static QDGetPicUPP gGetPICTData = NULL;
- OSErr anErr;
- Rect whereToDraw = *pWhereToDraw;
- PicHandle tempPict = (PicHandle) NewHandle(sizeof(Picture));
- #if USE_QD_GRAFPROCS
- CGrafPtr thePort = GetQDGlobalsThePort();
- #endif
-
- if (!gGetPICTData) {
- gGetPICTData = NewQDGetPicProc(GetPICTData);
- }
-
- anErr = MemError();
- nrequire(anErr, FailedNewHandle);
-
- // calculate the rectangle in which to draw, save the picture header into
- // our handle
- {
- PicPtr pPicture;
-
- pPicture = (PicPtr)((*sourceHandle) + sourceOffset);
- whereToDraw.right = whereToDraw.left + pPicture->picFrame.right - pPicture->picFrame.left;
- whereToDraw.bottom = whereToDraw.top + pPicture->picFrame.bottom - pPicture->picFrame.top;
- BlockMoveData((Ptr)pPicture, (Ptr)*tempPict, sizeof(Picture));
- }
-
- // store into globals for our GetPicProc in preparation for the draw
- gScrapHandle = sourceHandle;
- gCurrentOffset = sourceOffset + sizeof(Picture);
-
- #if USE_QD_GRAFPROCS
- // install our GetPic proc
- SetStdCProcs(&gMyColorProcs);
- gMyProcs.getPicProc = gGetPICTData;
- gMyColorProcs.getPicProc = gGetPICTData;
- GetPortGrafProcs(thePort, &gSavedProcs);
- SetPortGrafProcs(thePort, &gMyColorProcs);
- #endif
-
- // Draw the picture
- DrawPicture(tempPict, &whereToDraw);
-
- #if USE_QD_GRAFPROCS
- // remove our GetPic proc
- GetPortGrafProcs(thePort, &gSavedProcs);
- #endif
-
- DisposeHandle((Handle)tempPict);
-
- // FALL THROUGH EXCEPTION HANDLING
- FailedNewHandle:
- return anErr;
-
- } // DrawPictureFromHandleAndOffset
-
-
- //
- // Figure out the scrap type and offset
- //
- static long GetScrapTypeAndOffset( OSType * type, long * offset )
- {
- short i = 0;
- ResType scrapTypes[] = {'TEXT', 'PICT', '????'};
- long scrapResult = 0;
-
- while (scrapTypes[i] != '????')
- {
- scrapResult = GetScrap(nil, scrapTypes[i], offset);
- if (scrapResult > 0)
- {
- *type = scrapTypes[i];
- break;
- }
- ++i;
- }
-
- return scrapResult;
- }
-
- // --------------------------------------------------------------------------------------------------------------
- // OOP INTERFACE ROUTINES
- // --------------------------------------------------------------------------------------------------------------
- static OSErr ClipboardUpdateWindow(WindowRef pWindow, WindowDataPtr pData)
- {
- #pragma unused (pData)
-
- OSErr anErr;
- FontInfo theInfo;
- long scrapResult = 0;
- long offset;
- ResType validScrapType = '????';
- Rect topAreaRect;
- Rect bounds;
- RgnHandle oldClip = NewRgn();
- Rect eraseRect;
-
- GetWindowPortBounds( pWindow, &eraseRect );
-
- // clear out any data that was there before
- GetClip(oldClip);
- EraseRect(GetWindowPortBounds(pWindow, &bounds));
-
- // get that scrap!
- anErr = LoadScrap();
- nrequire(anErr, LoadScrap);
-
- // figure out the scrap type and offset
- scrapResult = GetScrapTypeAndOffset( &validScrapType, &offset );
-
- // setup for the drawing
- TextFont(applFont);
- TextSize(9);
- GetFontInfo(&theInfo);
-
- // caclulate our area at the top to say what type of contents the scrap is
- GetWindowPortBounds(pWindow, &topAreaRect);
- topAreaRect.bottom = topAreaRect.top + theInfo.ascent + theInfo.descent + theInfo.leading * 2 + 2;
-
- if( gMachineInfo.haveAppearanceMgr )
- {
- // draw a Finder-style window header
- Rect placardRect = topAreaRect;
-
- InsetRect( &placardRect, -1, -1 );
-
- DrawThemePlacard( &placardRect, IsWindowHilited(pWindow) ? kThemeStateActive : kThemeStateInactive );
- eraseRect.top = placardRect.bottom + 1;
- }
- else
- {
- // draw two lines under the area to separate it from the rest of the window
- MoveTo(topAreaRect.left, topAreaRect.bottom - 2);
- Line(topAreaRect.right - topAreaRect.left, 0);
- Move(0, 2);
- Line(-(topAreaRect.right - topAreaRect.left), 0);
- }
-
- EraseRect( &eraseRect );
-
- // draw a string describing the contents
- {
- Str255 theString;
-
- switch (validScrapType)
- {
- case 'PICT':
- GetIndString(theString, kClipboardStrings, iClipboardPICT);
- break;
-
- case 'TEXT':
- GetIndString(theString, kClipboardStrings, iClipboardText);
- break;
-
- default:
- if (InfoScrap()->scrapCount == 0)
- GetIndString(theString, kClipboardStrings, iClipboardNone);
- else
- GetIndString(theString, kClipboardStrings, iClipboardUnknown);
- break;
- }
-
- MoveTo(topAreaRect.left + 4, topAreaRect.bottom - 4);
-
- // draw in the correct placard text color ••• need to add deviceloop
- if( gMachineInfo.haveAppearanceMgr )
- {
- ThemeTextColor themeTextColor = kThemeTextColorPlacardActive;
-
- if( ! IsWindowHilited(pWindow) )
- {
- themeTextColor = kThemeTextColorPlacardInactive;
- }
-
- SetThemeTextColor( themeTextColor, 32, true );
- }
-
- DrawString(theString);
- }
-
- // calculate the part *not* in our top area
- topAreaRect.top = topAreaRect.bottom+1;
- topAreaRect.bottom = GetWindowPortBounds(pWindow, &bounds)->bottom;
-
- // remember the scrap count -- if it changes, we do an update!
- ((ClipboardDataPtr)pData)->scrapCount = InfoScrap()->scrapCount;
-
- // now, draw the contents, if we have a legal type to use
- {
- Rect clipArea = topAreaRect;
- Handle scrapHandle = InfoScrap()->scrapHandle;
-
- // scrollbar clear area - Donna doesn’t like this. She’s right- we have no scrollbars!
- // clipArea.right -= 15;
- // clipArea.bottom -= 15;
- ClipRect(&clipArea);
- switch (validScrapType)
- {
- case 'PICT':
- DrawPictureFromHandleAndOffset(&clipArea,
- scrapHandle, offset);
- break;
-
- case 'TEXT':
- {
- char oldState;
-
- oldState = HGetState(scrapHandle);
- HLock(scrapHandle);
- clipArea.right -= 15;
- clipArea.bottom -= 15;
- TETextBox(*scrapHandle+offset, scrapResult, &clipArea, teJustLeft);
- }
- break;
- }
- }
-
- // finally draw the grow icon, but omit our top area rect from the drawing
- ClipRect(&topAreaRect);
- DrawGrowIcon(pWindow);
-
- SetClip(oldClip);
- DisposeRgn(oldClip);
-
- UnloadScrap();
-
- // FALL THROUGH EXCEPTION HANDLING
- LoadScrap:
- return anErr;
-
- } // ClipboardUpdateWindow
-
-
- // --------------------------------------------------------------------------------------------------------------
-
- static Boolean ClipboardFilterEvent(WindowPtr pWindow, WindowDataPtr pData, EventRecord *pEvent)
- {
-
- // Force an update on scrap changes during activate/deactivate.
-
- switch (pEvent->what)
- {
- case nullEvent:
- if (LoadScrap() == noErr)
- {
- PScrapStuff pScrap = InfoScrap();
-
- if (pScrap->scrapCount != ((ClipboardDataPtr)pData)->scrapCount)
- {
- Rect bounds;
- InvalWindowRect(pWindow, GetWindowPortBounds(pWindow, &bounds));
- }
- }
- break;
-
- case activateEvt:
- {
- Rect bounds;
- InvalWindowRect(pWindow, GetWindowPortBounds(pWindow, &bounds));
- }
- break;
-
- // Follow the HI guidelines and hide the clipboard when we are suspended.
-
- case osEvt:
- if (((pEvent->message >> 24) & 0x0FF) == suspendResumeMessage)
- {
- if((pEvent->message & resumeFlag)==0) // suspending
- {
- HideWindow(pWindow);
- pWindow = FrontWindow();
- if (pWindow)
- HiliteWindow(pWindow, false);
- }
- else // resuming
- ShowWindow(pWindow);
- }
- break;
-
- } // switch(what)
-
- return false;
-
- } // ClipboardFilterEvent
-
-
- // --------------------------------------------------------------------------------------------------------------
-
- static OSErr ClipboardKeyEvent(WindowPtr pWindow, WindowDataPtr pData, EventRecord *pEvent, Boolean isMotionKey)
- {
- #pragma unused(pWindow, pData, pEvent, isMotionKey)
-
- return noErr;
-
- } // ClipboardKeyEvent
-
-
- // --------------------------------------------------------------------------------------------------------------
-
- static OSErr ClipboardGetBalloon(WindowPtr pWindow, WindowDataPtr pData,
- Point *localMouse, short * returnedBalloonIndex, Rect *returnedRectangle)
- {
- #pragma unused (pWindow, pData, localMouse, returnedRectangle)
-
- *returnedBalloonIndex = iNoBalloon;
-
- return noErr;
-
- } // ClipboardGetBalloon
-
- // --------------------------------------------------------------------------------------------------------------
-
- static OSErr ClipboardGetDocumentRect(WindowPtr pWindow, WindowDataPtr pData,
- LongRect * documentRectangle, Boolean forGrow)
- {
- #pragma unused (pWindow, pData, forGrow)
-
- OSType scrapType = 0;
- long sourceOffset;
- Rect theMaxSize;
-
- // default to size of main screen (••• sucks for text)
- GetPixBounds( (**GetMainDevice()).gdPMap, &theMaxSize );
-
- GetScrapTypeAndOffset( &scrapType, &sourceOffset );
-
- //
- // We know the size of a picture, man.
- //
- if( scrapType == 'PICT' )
- {
- if( LoadScrap() == noErr )
- {
- Handle sourceHandle = InfoScrap()->scrapHandle;
- PicPtr pPicture;
- FontInfo theInfo;
-
- pPicture = (PicPtr)((*sourceHandle) + sourceOffset);
- theMaxSize.right = pPicture->picFrame.right - pPicture->picFrame.left;
- theMaxSize.bottom = pPicture->picFrame.bottom - pPicture->picFrame.top;
-
- //
- // add window header size
- //
- TextFont(applFont);
- TextSize(9);
- GetFontInfo( &theInfo );
-
- theMaxSize.bottom += theInfo.ascent + theInfo.descent + theInfo.leading * 2 + 2;
-
- //
- // add width for scrollbars (••• which don’t exist)
- //
- // theMaxSize.bottom += 15;
- // theMaxSize.right += 15;
-
- UnloadScrap();
- }
-
- }
-
- RectToLongRect(&theMaxSize, documentRectangle);
-
- return noErr;
-
- } // ClipboardGetDocumentRect
-
-
- // --------------------------------------------------------------------------------------------------------------
-
- static OSErr ClipboardCloseWindow(WindowPtr pWindow, void* refCon)
- {
- #pragma unused(pWindow,refCon)
-
- ChangeCommandName(cShowClipboard, kClipboardStrings, iClipboardShow);
- UnloadScrap();
-
- return noErr;
-
- } // ClipboardCloseWindow
-
-
- // --------------------------------------------------------------------------------------------------------------
-
- static OSErr ClipboardMakeWindow(WindowPtr pWindow, WindowDataPtr pData)
- {
- #pragma unused (pWindow)
- BitMap bitmap;
-
- pData->hasGrow = true;
- pData->pFilterEvent = (FilterEventProc) ClipboardFilterEvent;
- pData->pKeyEvent = (KeyEventProc) ClipboardKeyEvent;
- pData->pGetBalloon = (GetBalloonProc) ClipboardGetBalloon;
- pData->pUpdateWindow = (UpdateWindowProc) ClipboardUpdateWindow;
- pData->pGetDocumentRect = (GetDocumentRectProc) ClipboardGetDocumentRect;
- pData->pCloseWindow = (CloseWindowProc) ClipboardCloseWindow;
-
- GetQDGlobalsScreenBits(&bitmap);
- pData->contentRect.right = pData->contentRect.left +
- bitmap.bounds.right -
- bitmap.bounds.left - 96;
- pData->contentRect.bottom = pData->contentRect.top + 150;
- MoveWindow(pWindow, bitmap.bounds.left + 4,
- bitmap.bounds.bottom - 154, false);
-
- ChangeCommandName(cShowClipboard, kClipboardStrings, iClipboardHide);
-
- return noErr;
-
- } // ClipboardMakeWindow
-
-
- // --------------------------------------------------------------------------------------------------------------
-
- OSErr ClipboardPreflightWindow(PreflightPtr pPreflightData)
- {
- pPreflightData->resourceID = kClipboardWindowID;
- pPreflightData->continueWithOpen = true;
- pPreflightData->makeProcPtr = ClipboardMakeWindow;
- pPreflightData->storageSize = sizeof(ClipboardDataRecord);
-
- return noErr;
-
- } // ClipboardPreflightWindow
-
- // --------------------------------------------------------------------------------------------------------------
-
- void ClipboardGetFileTypes(OSType * pFileTypes, OSType * pDocumentTypes, short * numTypes)
- {
- #pragma unused (pFileTypes, pDocumentTypes, numTypes)
-
- } // ClipboardGetFileTypes
-